﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;

namespace Zwms.Client.InfoWms
{
    public partial class GoodsForm : BaseForm
    {
        #region 成员变量
        public BaseGood CurrentGoods { get; set; }
        private EnumOpenType OpenType { get; set; }
        #endregion

        #region 构造函数
        public GoodsForm(EnumOpenType opentype, string sGoodsCode)
        {
            InitializeComponent();
            OpenType = opentype;

            if (opentype == EnumOpenType.New)
            {
                CurrentGoods = new BaseGood();
            }
            else if (opentype == EnumOpenType.Open)
            {
                tbGoodsCode.ReadOnly = true;
                //通过服务读取'入库计划明细'的实例
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                BaseGood record = new BaseGood();
                record.Id = sGoodsCode;

                CurrentGoods = client.QueryGoods(record);
            }
            InitBtnImage();
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnClear.Image = global::Zwms.Client.Properties.Resources.remove;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.exit;
        }

        private void InitControl()
        {
            if (this.OpenType == EnumOpenType.New)
            {
            }
            else if (this.OpenType == EnumOpenType.Open)
            {
                LoadInfo(CurrentGoods);
            }
        }

        private void LoadInfo(BaseGood record)
        {
            this.tbGoodsName.Text = record.GoodsName;
            this.tbShortName.Text = record.ShortName;
            
            this.tbModel.Text = record.Model;
            this.tbGoodsCode.Text = record.Id;
            this.tbBarCode.Text = record.BarCode;
            this.tbSingleGoodsCode.Text = record.SingleGoodsCode;
            this.nudSpec.Value = record.Specs;

            this.tbsGoodsCategory.SetTextBoxContent(record.CategoryName);
            this.tbsGoodsCategory.Tag = record.CategoryCode;
            this.tbsGoodsType.SetTextBoxContent(record.GoodTypeName);
            this.tbsGoodsType.Tag = record.GoodTypeCode;
            this.tbManufactor.Text = record.ManufactoryName;
            this.tbUPCBarCode.Text = record.UPCBarCode;
            this.nudRetailPrice.Value = record.RetailPrice;

            this.nudLength.Value = record.GoodsLength;
            this.nudWidth.Value = record.GoodsWidth;
            this.nudHeight.Value = record.GoodsHeight;
            this.nudPieceWeight.Value = record.PieceWeight;
            this.nudGoodsVolumn.Value = record.GoodsVolume;
            this.nudPieceVolumn.Value = record.PieceVolume;

            this.tbCreateUser.Text = record.CreateUserName;
            this.tbCreateDate.Text = Helper.CommonHelper.GetDatetimeFromString(record.CreateDate + record.CreateTime).ToLongDateString();
            this.tbEditUser.Text = record.EditUserName;
            this.tbEditDate.Text = Helper.CommonHelper.GetDatetimeFromString(record.EditDate + record.EditTime).ToLongDateString();
            this.tbRemark.Text = record.Remark;
        }
        #endregion

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //数据完整性检查
            bool bRet = true;
            if (this.OpenType == EnumOpenType.New)
            {
                if (tbGoodsCode.Text.Trim() == "")
                {
                    bRet = false;
                    tbGoodsCode.BackColor = Color.Yellow;
                }
            }

            if (tbGoodsName.Text.Trim() == "")
            {
                bRet = false;
                tbGoodsName.BackColor = Color.Yellow;
            }

            if (tbBarCode.Text.Trim() == "")
            {
                bRet = false;
                tbBarCode.BackColor = Color.Yellow;
            }

            if (!bRet)
            {
                return;
            }
            if (MessageBox.Show("您确定要添加（修改）货物信息吗？", "注意", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {

                BaseGood record = new BaseGood();
                record.Id = this.tbGoodsCode.Text;
                record.GoodsName = this.tbGoodsName.Text  ;
                record.ShortName = this.tbShortName.Text;
                record.Specs = Convert.ToInt32(this.nudSpec.Value);
                record.Model = this.tbModel.Text ;
                record.BarCode = this.tbBarCode.Text;
                record.SingleGoodsCode = this.tbSingleGoodsCode.Text;

                record.CategoryName = tbsGoodsCategory.GetTextBoxContent();
                if (tbsGoodsCategory.Tag != null)
                {
                    record.CategoryCode = tbsGoodsCategory.Tag.ToString();
                }
                record.GoodTypeName = tbsGoodsType.GetTextBoxContent();
                if (tbsGoodsType.Tag != null)
                {
                    record.GoodTypeCode = tbsGoodsType.Tag.ToString();
                }
                record.ManufactoryName = tbManufactor.Text;
                record.UPCBarCode = tbUPCBarCode.Text;
                record.RetailPrice = nudRetailPrice.Value;

                record.GoodsLength = nudLength.Value;
                record.GoodsWidth = nudWidth.Value;
                record.GoodsHeight = nudHeight.Value;
                record.PieceWeight = nudPieceWeight.Value;
                record.GoodsVolume = nudGoodsVolumn.Value;
                record.PieceVolume = nudPieceVolumn.Value;
                record.WeightUnit = "kg";
                record.VolumeUnit = "m3";
                record.LengthUnit = "m";

               ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client1.GetCurrentSystemTime();
                if (this.OpenType == EnumOpenType.New)
                {
                    record.CreateUserName = MainForm.CurrentUser.UserName;
                    record.CreateUserCode = MainForm.CurrentUser.Id;
                    record.CreateDate = Helper.CommonHelper.GetDateString(dtNow);
                    record.CreateTime = Helper.CommonHelper.GetTimeString(dtNow);
                    record.EditUserName = "";
                    record.EditUserCode = "";
                    record.EditDate = "";
                    record.EditTime = "";
                }
                else
                {
                    record.EditUserName = MainForm.CurrentUser.UserName;
                    record.EditUserCode = MainForm.CurrentUser.Id;
                    record.EditDate = Helper.CommonHelper.GetDateString(dtNow);
                    record.EditTime = Helper.CommonHelper.GetTimeString(dtNow);
                }

                record.Remark = tbRemark.Text;

                ServiceReferenceZwms.ServiceZwmsClient client2 = new ServiceReferenceZwms.ServiceZwmsClient();
                if (this.OpenType == EnumOpenType.New)
                {
                    bRet = client2.AddGoods(record);
                }
                else
                {
                    bRet = client2.UpdateGoods(record);
                }
                if (bRet)
                {
                    MessageBox.Show("成功!");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("未能添加，请检查编号是否已存在！");
                    return;
                }
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (this.OpenType == EnumOpenType.Open)
             {
                 this.tbGoodsName.Text = "";
             }
            this.tbShortName.Text = "";
            this.nudSpec.Value = 1;
            this.tbModel.Text = "";
            this.tbGoodsCode.Text = "";
            this.tbBarCode.Text = "";
            this.tbSingleGoodsCode.Text = "";
            this.nudSpec.Value = 0;
         
            this.tbsGoodsCategory.SetTextBoxContent("");
            this.tbsGoodsCategory.Tag = "";
            this.tbsGoodsType.SetTextBoxContent("");
            this.tbsGoodsType.Tag = "";
            this.tbManufactor.Text = "";
            this.tbUPCBarCode.Text = "";
            this.nudRetailPrice.Value = 0;

            this.nudLength.Value = 0;
            this.nudWidth.Value = 0;
            this.nudHeight.Value = 0;
            this.nudPieceWeight.Value = 0;
            this.nudPieceVolumn.Value = 0;
            this.nudGoodsVolumn.Value = 0;            
           
            this.tbRemark.Text = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void Control_Enter(object sender, EventArgs e)
        {
            this.Control_Enter(sender);
        }

        private void Control_Leave(object sender, EventArgs e)
        {
            this.Control_Leave(sender);
        }

        private void tbsGoodsCategory_SelectButtonClick(object sender, EventArgs e)
        {

            InfoWms.GoodsCategoryWnd uc = new InfoWms.GoodsCategoryWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "货物分类信息");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsGoodsCategory.SetTextBoxContent(uc.SelectedRecord.CategoryName);
                tbsGoodsCategory.Tag = uc.SelectedRecord.Id;
            }
        }
        #endregion

    }
}
